package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import pojo.Dept;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 徒有琴
 */
@Controller
@SessionAttributes(value = {"key1"})
public class ModelController {
    @RequestMapping("test1.html")
    public String test1(Map map) {
        map.put("key1", 123);
        map.put("key2", "abc");
        return "model";
    }

    @RequestMapping("test2.html")
    public String test2(Model model) {
        model.addAttribute("key1", 456);
        model.addAttribute("key2", "扣丁vip");
        return "model";
    }

    @RequestMapping("test3.html")
    public ModelAndView test3() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("key1", "789");
        modelAndView.addObject("key2", "mav");
        modelAndView.setViewName("model.jsp");
        return modelAndView;
    }


    @RequestMapping("test4.html")
    public String test4(@ModelAttribute String name, Dept dept) {
        System.out.println(name);
        System.out.println("dept.name=" + dept.getName());
        name = "aaa";
        //dept=new Dept();
        dept.setName("ddd");
        return "model";
    }

    @ModelAttribute("dept")
    public Dept getDept() {
        System.out.println("==========");
        Dept dept = new Dept();
        dept.setId(456);
        dept.setName("name");
        return dept;
    }

    /**
     * 在调用这个方法前，会执行getDept()
     * 如果请求中有参数，会覆盖掉getDept()的值
     * dept会作为数据模型返回到页面上
     */
    @RequestMapping("/test5.html")
    public String test5(@ModelAttribute Dept dept) {
        System.out.println(dept.getId());
        System.out.println(dept.getName());
        return "model";
    }

    @RequestMapping("/test6.html")
    public String test6(HttpServletRequest request) {
        request.setAttribute("key1", "000");
        return "model";
    }
}
